package com.ld.controller;

import com.ld.pojo.Classes;
import com.ld.pojo.PageResult;
import com.ld.pojo.Student;
import com.ld.pojo.StudentVo;
import com.ld.service.ClassesService;
import com.ld.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-08-26-16:33
 * @Description:
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;

    //条件查询并分页
    @RequestMapping("search")
    public String search(@RequestParam(defaultValue = "1",required = false) int page,
                         @RequestParam(defaultValue = "5",required = false) int pagesize,
                         StudentVo vo,
                         Model model){
        //分页查询学生
        PageResult<Student> pr = studentService.search(page,pagesize,vo);
        //放到model中
        model.addAttribute("pr",pr);
        //查询所有的班级
        List<Classes> classes = classesService.findAll();
        //将班级列表放天model
        model.addAttribute("classes",classes);
        model.addAttribute("vo",vo);
        model.addAttribute("page",page);
        //返回
        return "student/list";
    }

    //添加学生
    @PostMapping("add")
    public String add(Student student){
        System.out.println("student(add) = " + student);
        studentService.add(student);
        return "redirect:/student/search";
    }

    //修改学生
    @PutMapping("update")
    public String update(Student student){
        System.out.println("student(update) = " + student);
        studentService.update(student);
        return "redirect:/student/search";
    }

    //删除学生
    @GetMapping("delete/{sid}")
    public String delete(@PathVariable int sid){        //参数名相同就不用在注解后标明，否则需要在注解后标明取的值
        studentService.delele(sid);
        return "redirect:/student/search";
    }
}
